package org.judo.generate.mvc;

import org.judo.util.Util;

public class FormParser implements Parser
{
	ViewResult viewResult;
	private String curStatement;
	FormInfo formInfo;
	Parser nextParser;
	
	public ViewResult getViewResult()
	{
		return viewResult;
	}

	public void setViewResult(ViewResult viewResult)
	{
		this.viewResult = viewResult;
	}

	public Parser getNestedParser()
	{
		return nextParser;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/form"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{
		if (nextParser != null)
			return true;
		
		return false;
	}

	public void parseNextLine(String line) throws ParseException
	{
		nextParser = null;
		curStatement = line;
		
		if (line.startsWith("/form"))
		{
			startField();
			add("&nbsp;");
			space();
			add("<input type=\"submit\">");
			endField();
			viewResult.html += "\t\t\t\t</table>\n";
			viewResult.html += "\t\t\t</form>\n";
		}
		else if (line.equals(""))
		{
		}
		else if (line.startsWith("int:"))
		{
			doTextField(line,"int");
		}
		else if (line.startsWith("hidden:"))
		{
			String name = ParserUtil.getValue(line,viewResult);
			
			if (!name.contains(","))
			{
				viewResult.html += "\t\t\t\t<input type=\"hidden\" name=\""+name+"\" value=\"${"+name+"}\">\n";
				viewResult.contextVars.add(name);
			}
			else
			{
				String parts[] = name.split(",");
				
				if (parts.length == 2)
				{
				Util.out("2 parts");	
					viewResult.html += "\t\t\t\t<input type=\"hidden\" name=\""+parts[0]+"\" value=\"${"+parts[1]+"}\">\n";
					viewResult.contextVars.add(parts[0]);
				}
				else if (parts.length == 3)
				{
				Util.out("1 part");	
					viewResult.html += "\t\t\t\t<input type=\"hidden\" name=\""+parts[0]+"\" value=\"${"+parts[1]+"}\">\n";
					viewResult.contextVars.add(parts[2]+","+parts[0]);
				}
				else
				{
					throw new ParseException("Invalid input field declaration");
				}
			}		
		}
		else if (line.startsWith("string:"))
		{
			doTextField(line,"string");
		}
		else if (line.startsWith("float:"))
		{
			doTextField(line,"float");
		}
		else if (line.startsWith("date:"))
		{
			doTextField(line,"date");
		}
		else if (line.startsWith("path:"))
		{
			String path = ParserUtil.getValue(line,viewResult);
			formInfo.path = path.trim();
		}
		else if (line.startsWith("forward:"))
		{
			formInfo.forward = ParserUtil.getValue(line,viewResult);
		}
		else if (line.startsWith("populate:"))
		{
			formInfo.populate = true;
		}
		else if (line.startsWith("action:"))
		{
			ActionCodeParser parser = new ActionCodeParser();
			parser.setContainer(formInfo);
			nextParser = parser;
		}
	}

	protected void doTextField(String line,String type)
		throws ParseException
	{
		String value = ParserUtil.getValue(line,viewResult);
		String parts[] = value.split(",");
		
		if (parts.length != 2)
			throw new ParseException("Input Field definitions must be in the format '[type]:[fieldName],[displayText]'");
		
		startField();
		add("<b>"+parts[1].trim()+"</b>");
		space();
		
		if (formInfo.populate)
			add("<input type=\"text\" name=\""+parts[0].trim()+"\" value=\"${"+parts[0].trim()+"}\">");
		else
			add("<input type=\"text\" name=\""+parts[0].trim()+"\">");
		
		endField();
		
		formInfo.inputs.add(new InputInfo(parts[0].trim(),type));
	}
	
	protected void startField()
	{
		viewResult.html += "\t\t\t\t\t<tr>\n";
	}
	
	protected void endField()
	{
		viewResult.html += "\t\t\t\t\t</tr>\n";
		startField();
		space();
		space();
		space();
		viewResult.html += "\t\t\t\t\t</tr>\n";
	}
	
	
	
	protected void add(String controlHTML)
	{
		viewResult.html += "\t\t\t\t\t\t<td>\n";
		viewResult.html += "\t\t\t\t\t\t\t"+controlHTML+"\n";
		viewResult.html += "\t\t\t\t\t\t</td>\n";
	}
	
	protected void space()
	{
		viewResult.html += "\t\t\t\t\t\t<td>&nbsp;&nbsp;</td>\n";
	}
	
	public void setInitLine(String line) throws ParseException
	{
		formInfo = new FormInfo();
		viewResult.forms.add(formInfo);
		
		String value = ParserUtil.getValue(line,viewResult);
		value = value.trim();
		
		String parts[] = value.split("/");
		
		if (parts.length != 2)
			throw new ParseException("Form actions must be in the format [controller_name]/[view_name]");
		
		formInfo.controller = parts[0].trim();
		formInfo.action = parts[1].trim();
		
		if (viewResult.path != null)
			value = viewResult.path+"/"+value;  
				
		viewResult.html += "\t\t\t<form method=\"POST\" action=\"${rootContext}/"+value+".do\">\n";
		viewResult.html += "\t\t\t\t<table>\n";
	}
}
